/**
 * 语法 var p = new Proxy(target, handler);
 *   handler 代理操作详见: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Proxy/handler
 */
var obj = {
    prop: 'val'
};

var p = new Proxy(obj, {
    get(target, name) {
        console.log('get', target);
        console.log('get', name);
        return target[name] || '--';
    },
    set(target, name, value) {
        console.log('set', target);
        console.log('set', name);
        console.log('set', value);
        console.log('oldvalue', target[name]); // 不会呗 get 拦截
        target[name] = value;
        return true;
    }
});

p.prop = 'xx';
console.log(obj);

// 只读
function NOPE() {
    throw new Error("can't modify read-only view");
}
var handler = {
    // 覆写所有五种可变方法。
    set: NOPE,
    defineProperty: NOPE,
    deleteProperty: NOPE,
    preventExtensions: NOPE,
    setPrototypeOf: NOPE
};
function readOnly(target) {
    return new Proxy(target, handler);
}
var readOnlyObj = readOnly({name: 'readOnly'});
// readOnlyObj.prop = 1;  // Error: can't modify read-only view
console.log(readOnlyObj);